package fiuba.service.impl;

import org.springframework.dao.DataIntegrityViolationException;

import fiuba.service.impl.UniversalManagerImpl;
import fiuba.model.LabelValue;
import fiuba.model.Menu;
import fiuba.model.MenuItem;
import fiuba.service.MenuManager;
import fiuba.service.MenuExistsException;
import fiuba.dao.MenuDao;
import fiuba.dao.MenuItemDao;

import java.util.List;

import javax.jws.WebService;
import javax.persistence.EntityExistsException;

@WebService(serviceName = "MenuService", endpointInterface = "fiuba.service.MenuManager")
public class MenuManagerImpl extends UniversalManagerImpl implements
		MenuManager {
    private MenuDao dao;
    private MenuItemDao menuItemDao;


public void setMenuDao(MenuDao dao){
	this.dao = dao;
}

public void setMenuItemDao(MenuItemDao menuItemDao){
	this.menuItemDao = menuItemDao;
}

public Menu getMenu (String menuId){
	return dao.get(new Long(menuId));
}

public Menu saveMenu  (Menu menu) throws MenuExistsException {
        
        menu.setName(menu.getName().toLowerCase());
        
        try {
            return dao.saveMenu(menu);
        } catch (DataIntegrityViolationException e) {
            e.printStackTrace();
            log.warn(e.getMessage());
            throw new MenuExistsException("Menu '" + menu.getName() + "' already exists!");
        } catch (EntityExistsException e) { // needed for JPA
            e.printStackTrace();
            log.warn(e.getMessage());
            throw new MenuExistsException("Menu '" + menu.getName() + "' already exists!");
        }
    }

public void removeMenu (String menuId) {
	log.debug("removing Menu with id:" + menuId);
	dao.remove(new Long(menuId));
}

public List <Menu> getMenus(){
	return dao.getMenus();	
}

public List <LabelValue> getMenuItemsNotInMenu(Menu menu) {
	
	return dao.getOtherMenuItems (menu);
}

public String printMenu (Menu menu){
	StringBuilder content = new StringBuilder();
	
	
	/* CSS de menu, necesario ponerlo aca para que sea configurable */
	content
	.append("<style type='text/css'>")
	.append("\na.mainlevel:link,").append("\na.mainlevel:active,").append("\na.mainlevel:visited {");
	if (!menu.getColorItem().equals("")) {
		content.append("\ncolor:").append(menu.getColorItem()).append(";");
	}
	if (!menu.getTamanioFuenteItem().equals("")) {
		content.append("\nfont-size:").append(menu.getTamanioFuenteItem()).append("px;");
	}
	if (!menu.getFuenteItem().equals("")) {
		content.append("\nfont-family:").append(menu.getFuenteItem()).append(";");
	}
	if (!menu.getColorFondoItem().equals("")) {
		content.append("\nbackground-color:").append(menu.getColorFondoItem()).append(";");
	}
	if (!menu.getAltoMenuItem().equals("")) {
		content.append("\nline-height:").append(menu.getAltoMenuItem()).append("px;");
	}
	content.append("\ntext-decoration:none;")
	.append("\n}")
	.append("\na.mainlevel:hover{");
	if (!menu.getColorItemHover().equals("")) {
		content.append("\ncolor:").append(menu.getColorItemHover()).append("!important;");
	}
	if (!menu.getTamanioFuenteItemHover().equals("")) {
		content.append("\nfont-size:").append(menu.getTamanioFuenteItemHover()).append("px;");
	}
	if (!menu.getFuenteItem().equals("")) {
		content.append("\nfont-family:").append(menu.getFuenteItem()).append(";");
	}
	if (!menu.getColorFondoItemHover().equals("")) {
		content.append("\nbackground-color:").append(menu.getColorFondoItemHover()).append(";");
	}
	
	content.append("\ntext-decoration:none!important;")
		.append("\n}")
	.append("\n.mainlevel  {")
		.append("\ndisplay:block;")
		.append("\nfont-weight:bold;")
		.append("\npadding-left:15px;")
		.append("\ntext-decoration:none;")
	.append("\n}")
	.append("</style>");	
	
	
	
	content.append("\n\n<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border='0'>").
	append("<tbody>");
	
	String[] arrayOrden = menu.getOrden().split(",");
	for (int i = 0; i < arrayOrden.length;i++) {
		Long idActual = Long.parseLong(arrayOrden[i]);
		for (MenuItem item : menu.getMenuItems()){
			if (item.getId().equals(idActual) 
					&& item.getSeccion().getPublicada()){
				content.append("\n<tr align=\"left\">")
				.append("<td>")
				.append("<a class=\"mainlevel\" ")
				.append("href=\"/verSeccion.html?Id=" + item.getSeccion().getId() + "\">" + item.getName() + "</a>")
				.append("</td></tr>");
			}		
		}	
	}
	
	
	content.append("\n</tbody></table>");
	return content.toString();
}

}
